package com.lumacong.websocket.timer;

import com.alibaba.fastjson.JSON;
import com.lumacong.websocket.server.DsPrizeServer;
import com.lumacong.websocket.service.DsIntegralTodayService;
import com.lumacong.websocket.service.DsPrizeService;
import com.lumacong.websocket.service.MysqlService;
import com.lumacong.websocket.vo.DsIntegralToday;
import com.lumacong.websocket.vo.DsPrize;
import com.sun.management.OperatingSystemMXBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@EnableScheduling
public class DsPrizeTask {
    @Autowired
    MysqlService mysqlService;
    @Autowired
    DsPrizeService dsPrizeService;
    SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

    private static List<String> dates = new ArrayList<>();

    static {

    }

    final DataList dataList = new DataList();

    @Scheduled(cron = "0/3 * * * * ?") // 每分钟执行一次
    public void test() {
        /*System.err.println("*********   定时任务执行   **************");*/
        CopyOnWriteArraySet<DsPrizeServer> mysqlSocketServer = DsPrizeServer.getWebSocketSet();
        mysqlSocketServer.forEach(c -> {
            try {
                List<DsPrize> all = dsPrizeService.findAll();
                String msg = JSON.toJSONString(all);
                c.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }


    class DataList {

        private List<String> arr;
        private Map<String, Object> obj = new HashMap<>();

        private AtomicInteger atomicInteger = new AtomicInteger(0);

        public List<String> getArr() {
            return arr;
        }

        public void setArr(List<String> arr) {
            this.arr = arr;
        }

        public Map<String, Object> getObj() {
            return obj;
        }

        public void setObj(Map<String, Object> obj) {
            this.obj = obj;
        }

        public Integer leftPop() {
            return atomicInteger.incrementAndGet();
        }
    }
}
